"""
Transaction model for the trading platform.
"""

from sqlalchemy import Column, Integer, String, DECIMAL, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base


class Transaction(Base):
    """Transaction model representing financial transactions."""

    __tablename__ = 'transactions'

    id = Column(Integer, primary_key=True, autoincrement=True)
    trade_id = Column(Integer, ForeignKey('trades.id'), nullable=True)
    wallet_id = Column(Integer, ForeignKey('wallets.id'), nullable=False)
    amount = Column(DECIMAL(10, 2), nullable=False)
    transaction_type = Column(String(20), nullable=False)  # deposit, withdrawal, payment, refund, commission
    status = Column(String(20), nullable=True)  # pending, completed, failed, cancelled
    transaction_no = Column(String(64), unique=True, nullable=False)
    remark = Column(String(255), nullable=True)
    created_at = Column(DateTime, default=func.now())

    # Relationships
    wallet = relationship("Wallet", back_populates="transactions")
    trade = relationship("Trade", back_populates="transactions")

    def __repr__(self):
        return f"<Transaction(id={self.id}, wallet_id={self.wallet_id}, amount={self.amount}, type={self.transaction_type})>"